<template>
  <page-header-wrapper :title="false">
    <a-card
      :loading="loading"
    >
      <div class="table-page-search-wrapper">
        <a-form-model :model="queryParam" layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="顾问姓名">
                <a-select v-model="queryParam['filter[adviser_id]']">
                  <a-select-option v-for="item in userOpts" :key="item.uid" :value="item.uid">{{item.username}}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="按月搜索">
                <a-select v-model="queryParam['filter[type]']">
                  <a-select-option value="today">今日</a-select-option>
                  <a-select-option value="month">当月</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="选择时间">
                  <a-range-picker @change="onSubmitTime" />
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="onSearch">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operations">
        <a-table
          bordered
          :row-key="(record) => record.uid"
          :data-source="stateData"
          :pagination="false"
          :scroll="{ y: 500 }"
        >
          <a-table-column title="顾问姓名" data-index="adviser_name"/>
          <a-table-column-group title="业绩">
            <a-table-column title="计划" data-index="target_currmonth_yj" :sorter="(a, b) => a.target_currmonth_yj - b.target_currmonth_yj">
              <template slot-scope="target_currmonth_yj">
                <a-tag color="red">{{ target_currmonth_yj }}</a-tag>
              </template>
            </a-table-column>
            <a-table-column title="完成" data-index="yj" :sorter="(a, b) => a.yj - b.yj">
              <template slot-scope="yj">
                <a-tag color="red">{{ yj }}</a-tag>
              </template>
            </a-table-column>
            <a-table-column title="差值" data-index="yjc" :sorter="(a, b) => a.yjc - b.yjc">
              <template slot-scope="yjc">
                <a-tag color="red">{{ yjc }}</a-tag>
              </template>
            </a-table-column>
          </a-table-column-group>
          <a-table-column-group title="通话">
            <a-table-column title="计划" data-index="target_dacall_sc" :sorter="(a, b) => a.target_dacall_sc - b.target_dacall_sc">
              <template slot-scope="target_dacall_sc">
                <a-tag color="red">{{ target_dacall_sc }}</a-tag>
              </template>
            </a-table-column>
            <a-table-column title="完成" data-index="ts" :sorter="(a, b) => a.ts - b.ts">
              <template slot-scope="ts">
                <a-tag color="red">{{ ts }}</a-tag>
              </template>
            </a-table-column>
            <a-table-column title="差值" data-index="tsc" :sorter="(a, b) => a.tsc - b.tsc">
              <template slot-scope="tsc">
                <a-tag color="red">{{ tsc }}</a-tag>
              </template>
            </a-table-column>
          </a-table-column-group>
          <a-table-column-group title="领新">
            <a-table-column title="完成" data-index="num" :sorter="(a, b) => a.num - b.num">
              <template slot-scope="num">
                <a-tag color="red">{{ num }}</a-tag>
              </template>
            </a-table-column>
          </a-table-column-group>
          <a-table-column-group title="共享库">
            <a-table-column title="完成" data-index="gx_num" :sorter="(a, b) => a.gx_num - b.gx_num">
              <template slot-scope="gx_num">
                <a-tag color="red">{{ gx_num }}</a-tag>
              </template>
            </a-table-column>
          </a-table-column-group>
        </a-table>
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
  import { getState } from '@/api/state'
  import col from '@/columns/state'

  export default {
    name: 'State',
    data () {
      return {
        stateColumns: col.state,
        stateData: [],
        stateYj: [],
        stateTs: [],
        stateLx: [],
        stateGx: [],
        queryParam: {},
        loading: false,
        advanced: true
      }
    },
    created () {
      this.getState(this.queryParam)
    },
    methods: {
      /**
       * 获取全局参数
       * 看到这段代码不要骂程序员是傻逼
       * 因为是老板那个傻逼要求这样写的
       * 虽然逻辑确实很傻逼
       * @param params
       */
      getState (params) {
        const app = this
        const data = {}
        const arr = []
        this.loading = true
        params['page_size'] = 150
        params['channel'] = 'yj'
        getState(params).then(yj => {
          app.stateYj = yj.result.data
          app.userOpts = yj.result.user_opts
          params['channel'] = 'ts'
          getState(params).then(ts => {
            app.stateTs = ts.result.data
            params['channel'] = 'lx'
            getState(params).then(lx => {
              app.stateLx = lx.result.data
              params['channel'] = 'gx'
              getState(params).then(gx => {
                app.stateGx = gx.result.data
                for (const k in app.stateYj) {
                  data[k] = app.stateYj[k]
                  data[k].yjc = app.stateYj[k].target_currmonth_yj - app.stateYj[k].yj
                  for (const j in app.stateTs) {
                    if (data[k].uid === app.stateTs[j].uid) {
                      data[k].target_dacall_sc = app.stateTs[j].target_dacall_sc
                      data[k].ts = app.stateTs[j].ts
                      data[k].leader_name = app.stateTs[j].leader_name
                      data[k].tsc = app.stateTs[j].target_dacall_sc - app.stateTs[j].ts
                    }
                  }
                  for (const j in app.stateLx) {
                    if (data[k].uid === app.stateLx[j].uid) {
                      data[k].num = app.stateLx[j].num
                    }
                  }
                  for (const j in app.stateGx) {
                    if (data[k].uid === app.stateGx[j].uid) {
                      data[k].gx_num = app.stateGx[j].num
                    }
                  }
                  arr.push(data[k])
                }
                app.stateData = arr
                app.loading = false
              })
            })
          })
        })
      },
      filterOption (input, option) {
        return (option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0)
      },
      onSearch () {
        this.getState(this.queryParam)
      },
      onSubmitTime (date, dateString) {
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      }
    }
  }
</script>

<style scoped>

</style>
